﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.ComponentModel;

namespace ICeFramework.Common.Configuration
{
    public class DataAccessSectionHandler : ConfigurationSection
    {
        public const string SectionName = "dataAccess";
        public const string ConnectionName = "connection";

        private static readonly ConfigurationProperty _connections;
        private static readonly ConfigurationPropertyCollection _properties;

        #region static DataAccessSectionHandler()
        /// <summary>
        /// Initializes a new instance of the <b>DataAccessSectionHandler</b> class.
        /// </summary>
        static DataAccessSectionHandler()
        {
            _connections = new ConfigurationProperty(null, typeof(ConnectionPropertyCollection), null, ConfigurationPropertyOptions.IsDefaultCollection);
            _properties = new ConfigurationPropertyCollection();
            _properties.Add(_connections);
        }
        #endregion

        #region public ConnectionElement DefaultConnection
        /// <summary>
        /// Gets the DefaultConnection of the DataAccessSectionHandler
        /// </summary>
        /// <value></value>
        public ConnectionElement DefaultConnection
        {
            get { return GetDefaultConnection(); }
        }
        #endregion

        #region private ConnectionElement GetDefaultConnection()
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private ConnectionElement GetDefaultConnection()
        {
            foreach (ConnectionElement elem in this.Connections)
            {
                if (elem.IsDefault)
                {
                    return elem;
                }
            }
            throw new ConfigurationErrorsException("There is no default dataAccess item specified.");
        }
        #endregion

        #region protected override ConfigurationPropertyCollection Properties
        /// <summary>
        /// Gets the Properties of the DataAccessSectionHandler
        /// </summary>
        /// <value></value>
        protected override ConfigurationPropertyCollection Properties
        {
            get
            {
                return _properties;
            }
        }
        #endregion

        #region public ConnectionPropertyCollection Connections
        /// <summary>
        /// Gets the Connections of the DataAccessSectionHandler
        /// </summary>
        /// <value></value>
        public ConnectionPropertyCollection Connections
        {
            get { return (ConnectionPropertyCollection)base[_connections]; }
        }
        #endregion
    }
}
